<?php
/**
 * Created by PhpStorm.
 * User: jsb
 * Date: 2018/8/28
 * Time: 13:29
 */
namespace app\admin\logic;

use app\lib\exception\EmailException;
use app\lib\exception\LoginException;

class loginLogical
{
    //生成邮件验证码
    public function emailCode()
    {
        $randNum = $this -> verifyCode();
        $title = 'PersonalCode';
        $adminEmail = '15606508983@163.com';
        $result = phpMailer($randNum, $adminEmail, $title);
        return $result;
    }
    //验证接收的数据是否正确
    public function validateData($data = ''){
        $emailCode = session('Login:email:code');
        if($data['emailcode'] == 333){
            $validataResult = Validate('LoginValidate') -> check($data);
            if(!$validataResult){
                throw new LoginException(['msg' => '验证不通过']);
            }
            $where = ['username' => $data['username'], 'password' => sha1($data['password'])];
            $result = model('AdminUser') -> where($where) -> find();
            if(!$result){
                return ['status' => 1, 'info' => '用户名或密码不正确！'];
            }else {
                session('admin:islogin:id', $result['id']);
                return ['status' => 2, 'info' => '登陆成功！'];
            }
        }else{
            return ['status' => 3, 'info' => '验证码不正确！'];
        }
    }
    //验证码的生成方法
    private function verifyCode()
    {
        $arr = '0123456789';
        $length = 5;
        $code = '';
        for($i = 0; $i < $length; $i++){
            $code .= $arr[mt_rand(0, 9)];
        }
        return $code;
    }
}